import {Component} from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {STORAGE_KEY} from "../../app/app.general";
import {Login} from "../pages";
import {LoginService} from "../login/login.service";
import {AppConfig} from "../../app/appConfig";
import {ACLService} from "../../app/acl/acl.service";

/**
 * Generated class for the SettingsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-settings',
  templateUrl: 'settings.html',
})
export class SettingsPage {
  private items: any;

  constructor(public navCtrl: NavController, public navParams: NavParams, private app: App, public loginService: LoginService, public appConfig: AppConfig, public acl: ACLService) {
    this.items = [
      {name: '修改密码', val: 'changePassword'},
      {name: '退出登录', val: 'logout'},
    ];
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingsPage');
  }

  valHandler(val) {
    switch (val) {
      case 'logout':

        this.appConfig.presentLoading('正在登出...');
        this.acl.clearAll();
        localStorage.setItem(STORAGE_KEY.token, '');

        this.app.getRootNav().setRoot(Login, {}, {animate: true, direction: 'enter'}).then(() =>
          this.appConfig.loader.dismiss()
        );

        break;
      case 'changePassword':
        this.navCtrl.push('ResetPassowrdPage');
        break;
    }
  }

}
